package com.oto.web.resource;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author xueqiang.mi
 * @since 2011-1-9
 */
public abstract class PostDataSupport {

	public static final String URL = "http://www.otophone.com/mobile/share";

	public void share(String data) throws IOException {
		HttpURLConnection conn = null;
		OutputStream out = null;
		InputStream in = null;
		try {
			URL url = new URL(URL);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setUseCaches(false);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			conn.connect();
			out = conn.getOutputStream();
			OutputStreamWriter writer = new OutputStreamWriter(out);
			writer.write("data=" + data);
			writer.flush();
			writer.close();

			if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
				System.out.println("Error occurs. ResponseCode: " + conn.getResponseCode());
			}
			byte[] buffer = new byte[32];
			in = conn.getInputStream();
			in.read(buffer);
			System.out.println(new String(buffer));
		} finally {
			if (out != null) {
				out.close();
			}
			if (in != null) {
				in.close();
			}
			conn.disconnect();
		}
	}
}
